//
//  forgetPasswordViewController.m
//  one_mile_01
//
//  Created by 王进帅 on 15/8/28.
//  Copyright (c) 2015年 王雅蓉. All rights reserved.
//

#import "forgetPasswordViewController.h"

@interface forgetPasswordViewController ()

@property (nonatomic, strong)UITextField *text;

@property (nonatomic, strong)UITextField *PassWordTF;

@end

@implementation forgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BACKGROUND_ASSISTANT_COLOR;
    
    UIView *navigationV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, NAVIGATIONHEIGHT)];
    navigationV.backgroundColor = BACKGROUND_MAIN_COLOR;
    [self.view addSubview:navigationV];
    
    //    返回按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = POP_FRAME;
    [button setBackgroundImage:[UIImage imageNamed:@"pop_dark.png"] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor clearColor];
    [button addTarget:self action:@selector(backbutton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
    self.navigationController.navigationBar.hidden = YES;
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:NAVIGATION_TITLE_FRAME];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = NAVIGATION_TITLE_FONT;
    titleLabel.text = @"忘记密码";
    [self.view addSubview:titleLabel];

    
//    小手机
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(titleLabel.frame.origin.x - 20 * FITRATE, titleLabel.frame.origin.y + 80 * FITRATE, 20 * FITRATE, 20 * FITRATE)];
    imageView.image = [UIImage imageNamed:@"loginID.png"];
    [self.view addSubview:imageView];
    
//    注册手机邮箱
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(imageView.frame.origin.x + imageView.frame.size.width + 5, imageView.frame.origin.y - 5, 200 * FITRATE, 30 * FITRATE)];
    label1.text = @"注册手机/邮箱";
    label1.textColor = [UIColor lightGrayColor];
    label1.font = [UIFont systemFontOfSize:15 * FITRATE];
    [self.view addSubview:label1];
    
    self.text = [[UITextField alloc]initWithFrame:CGRectMake(button.frame.origin.x + 10 * FITRATE , label1.frame.origin.y + label1.frame.size.height + 15 * FITRATE, self.view.frame.size.width - button.frame.origin.x * 2 - 20 * FITRATE, 40 * FITRATE)];
    self.text.layer.masksToBounds = YES;
    self.text.layer.borderWidth  = 1.0;
    self.text.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [self.view addSubview:self.text];
    
//    新的密码
    UILabel  *newPassLabel = [[UILabel alloc]initWithFrame:CGRectMake(label1.frame.origin.x, self.text.frame.origin.y + self.text.frame.size.height + 20 * FITRATE, label1.frame.size.width, label1.frame.size.height)];
    newPassLabel.text = @"新的密码";
    newPassLabel.textColor = [UIColor lightGrayColor];
    newPassLabel.font = [UIFont systemFontOfSize:15 * FITRATE];
    [self.view addSubview:newPassLabel];

//   新的密码输入框
    self.PassWordTF = [[UITextField alloc]initWithFrame:CGRectMake(self.text.frame.origin.x, newPassLabel.frame.origin.y + newPassLabel.frame.size.height + 10 * FITRATE, self.view.frame.size.width - button.frame.origin.x * 2 - 20 * FITRATE, 40 * FITRATE)];
    self.PassWordTF.layer.masksToBounds = YES;
    self.PassWordTF.layer.borderWidth  = 1.0;
    self.PassWordTF.secureTextEntry = YES;
    self.PassWordTF.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [self.view addSubview:self.PassWordTF];
    
//    验证码框
    self.testTextF = [[UITextField alloc]initWithFrame:CGRectMake(self.PassWordTF.frame.origin.x ,self.PassWordTF.frame.origin.y + self.PassWordTF.frame.size.height + 40  * FITRATE, self.PassWordTF.frame.size.width / 5 * 3 - 10 * FITRATE , 40 * FITRATE)];
    self.testTextF.layer.masksToBounds = YES;
    self.testTextF.layer.borderWidth = 1.0;
    self.testTextF.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.testTextF.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.testTextF];
    
//   验证码
    self.testButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.testButton.frame =CGRectMake(self.testTextF.frame.origin.x + self.testTextF.frame.size.width + 20,self.testTextF.frame.origin.y , self.PassWordTF.frame.size.width / 5 * 2 - 10  * FITRATE, 40);
    [self.testButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.testButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    self.testButton.layer.masksToBounds = YES;
    self.testButton.titleLabel.font = [UIFont systemFontOfSize:15 * FITRATE];
    self.testButton.layer.borderWidth = 1.0;
    self.testButton.layer.cornerRadius = 6;
    self.testButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [self.testButton addTarget:self action:@selector(testButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.testButton];
    
    UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(self.text.frame.origin.x,self.testButton.frame.size.height +self.testButton.frame.origin.y + 30 * FITRATE,self.text.frame.size.width,self.text.frame.size.height)];
    button1.backgroundColor = [UIColor colorWithRed:71/255.0 green:172/255.0 blue:226/255.0 alpha:1.0];
    button1.layer.masksToBounds = YES;
    button1.layer.cornerRadius = 10;
    [button1 setTitle:@"找回密码" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(lookBackPassWordAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    // Do any additional setup after loading the view.
}

-(void)testButtonAction:(UIButton *)button{

[self.testButton setTitle:@"正在发送..." forState:UIControlStateNormal];
[self.testButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self getCheckNo:self.text.text];
}

-(void)getCheckNo:(NSString *)username
{
    NSString *urlStr = @"http://service.1yingli.cn/yiyingliService/manage";
    
    [AFNConnect AFNConnectWithUrl:urlStr withBodyData:[AFNConnect createDataForCheckNo:self.text.text] connectBlock:^(id data) {
        
        NSDictionary *dic = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSLog(@"%@", dic);
        NSString *state = [dic objectForKey:@"state"];
        
        if ([state isEqualToString:@"success"]) {
            
            [self setAlertView:@"提示" withMessage:@"验证码已发送成功，注意查收验证码" buttonTitle:@"确认"];
            [self.testButton setTitle:@"获取验证码" forState:UIControlStateNormal];
            [self.testButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            [self.testButton setBackgroundColor:[UIColor clearColor]];
        } else {
            
            [self setAlertView:@"提示" withMessage:@"验证码获取失败" buttonTitle:@"确认"];
            
            [self.testButton setTitle:@"获取验证码" forState:UIControlStateNormal];
            [self.testButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            [self.testButton setBackgroundColor:[UIColor clearColor]];
        }
    }];
}

#pragma mark -- 提示框
-(void)setAlertView:(NSString *)title withMessage:(NSString *)message buttonTitle:(NSString *)buttonTitle
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:buttonTitle otherButtonTitles:nil];
    [self.view addSubview:alert];
    
    [alert show];
}

//找回密码
-(void)lookBackPassWordAction:(UIButton *)button{

    NSString *urlStr = @"http://service.1yingli.cn/yiyingliService/manage";
[AFNConnect AFNConnectWithUrl:urlStr withBodyData:[AFNConnect createDataForForgetPassword:self.text.text withPWD:self.PassWordTF.text withCheckNo:self.testTextF.text] connectBlock:^(id data) {

    NSDictionary *dic = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    if ([[dic objectForKey:@"state"] isEqualToString:@"success"]) {
        
        if ([[dic objectForKey:@"msg"] isEqualToString:@"password has changed"]) {
            
            [self setAlertView:@"提示" withMessage:@"修改成功" buttonTitle:@"确认"];
            
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        }
        
    } else if([[dic objectForKey:@"state"] isEqualToString:@"error"]){
        
    if ([[dic objectForKey:@"msg"] isEqualToString:@"it is not a phone No or email"]){
    
        [self setAlertView:@"提示" withMessage:@"邮箱或手机格式错误" buttonTitle:@"确认"];
    }else if ([[dic objectForKey:@"msg"] isEqualToString:@"checkNo is wrong"]){
        
        [self setAlertView:@"提示" withMessage:@"验证码错误" buttonTitle:@"确认"];
    }else if ([[dic objectForKey:@"msg"] isEqualToString:@"checkNo is overdue"]){
        
        [self setAlertView:@"提示" withMessage:@"验证码过期" buttonTitle:@"确认"];
    }else if ([[dic objectForKey:@"msg"] isEqualToString:@"phone or email has not been registered"]){
        
        [self setAlertView:@"提示" withMessage:@"该账号尚未注册" buttonTitle:@"确认"];
    }else if ([[dic objectForKey:@"msg"] isEqualToString:@"phone or email has not been registered"]){
        
        [self setAlertView:@"提示" withMessage:@"密码不合乎规范" buttonTitle:@"确认"];
    }else if ([[dic objectForKey:@"msg"] isEqualToString:@"error"]){
        
        [self setAlertView:@"提示" withMessage:@"服务器异常" buttonTitle:@"确认"];
    }else if ([[dic objectForKey:@"msg"] isEqualToString:@"data is incomplete"]){
        
        [self setAlertView:@"提示" withMessage:@"请认真填写所有信息" buttonTitle:@"确认"];
      }
    }
}];
    

}


-(void)backbutton:(UIButton *)button{

[self dismissViewControllerAnimated:YES completion:^{
    
}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
